<?php
function sopc($tasks, $url = "www.so.com")
{

    Global $whitelist; /*白名单*/
    Global $posturl;
    Global $filepath;   /*开关文件*/
    $nul = 0;   /*快照为空*/
    //网站域名

    $header = getheader($url);
    /*每页搜索记录*/
    for ($pn = 0; $pn < $tasks['pn']; $pn++) {

        $url = "https://www.so.com/s?q=" . str_replace(' ', '%20', $tasks['search']) . "&pn=" . ($pn + 1);

        print_f($url . "\t");

        $contents = Get_Web_Contents($url, 'GET', '', '', $header);/*360搜索结果页*/

        $text = str_replace(array("\r\n", "\r", "\n"), "", $contents['Body']);/*合并一行*/

        $text = preg_replace("/<(style|script)(.*?)<\/(style|script)>/i", "", $text);//去css和js
        /*修改360验证规则*/
        preg_match_all('/("res-linkinfo">(.*?)快照<\/a>)/', $text, $content);
        preg_match('/果约(.*?)个/', $text, $nums);


        /* if(empty($content[0])) {
        //     //0结果
        //     preg_match('/class="nors"(.*?)<\/div>/',$contents['Body'],$content);
        //     if(!empty($content[0])){
        //         print_f("0结果");
        //         return;
        //     }
        //     //出验证码了
        //     preg_match('/verifycode(.*?)<\/form>/',$contents['Body'],$content);
        //     if(!empty($content[0])){
        //         print_f("百度验证码");
        //         file_put_contents($filepath,"End");
        //         exit;
        //     }
        // }*/
        echo count($content[0]) . "\n";/*每页搜索结果*/
        /*每条搜索记录*/
        foreach ($content[0] as $k => $v) {

            /*取网站域名*/
            preg_match("/>([^\/]+\.)?({$whitelist})\//", $v, $url);

            /*白名单*/
            if (!empty($url)) {
                echo "whitelist \n";
                continue;
            }

            /*取网站快照*/
            preg_match('/ href="h(.*?)\"/', $v, $url);

            echo $k . "\t";

            /*没快照*/
            if (empty($url[1])) {
                preg_match('/url":"h(.*?)\"/', $v, $url);
            }
            /*快照*/
            $Url = 'h' . $url[1];
            $finalsnapshot = Get_Web_Contents($Url, 'GET', '', '', $header);
            preg_match("#URL='(.*?)'#", $finalsnapshot['Body'], $url);
            $header[] = 'Referer:' . $Url;
            $finalsnapshot = Get_Web_Contents($url[1], 'GET', '', '', $header);
            /*快照为空*/
            if (empty($finalsnapshot['Body'])) {
                $nul++;
                print_f($finalsnapshot['Error'] . "\n");
                if ($nul > 10) {
                    $nul = 0;
                    sleep(600);
                }
                continue;
            }

            $snapshot = str_replace(array("\r\n", "\r", "\n", " ", "\t"), "", $finalsnapshot['Body']);

            preg_match('/<title(.*?)>(.*?)<\/title>/i', $snapshot, $data);

            $snapshot = codeWords($snapshot);/*命中词加样式*/

            if (!empty($snapshot)) {

                print_f($data[2] . "\n");
                /*判断快照是否重复*/
                $insert['tasks_id'] = $tasks['id'];
                $insert['tasks_name'] = $tasks['name'];
                $insert['search'] = $tasks['search'];
                $insert['keyword'] = $snapshot['count'];
                $insert['title'] = $data[2];
                $insert['url'] = $url[1];
                $insert['pn'] = $pn + 1;
                $insert['po'] = $k + 1;
                $insert['status'] = 1;
                $insert['addtime'] = time();
                $insert['snapshot'] = $snapshot['snapshot'];
                /*提交*/
                Get_Web_Contents($posturl, 'POST', $insert);

            } else {
                print_f("未命中\n");
            }
        }

        echo "\n";

        /*页数超出最大结果值*/
        if ((($pn + 1) * 10) > str_replace(',', '', $nums[1]))
            return;
    }
}

